package com.xbh.management.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbh.common.base.BaseEntity;
import com.xbh.common.utils.JwtUtil;
import com.xbh.management.enums.LeaveStatusEnum;
import com.xbh.management.mapper.ManLeaveMapper;
import com.xbh.management.model.ManLeave;
import com.xbh.notice.api.SendMessageFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2022-03-07 10:27
 */

@Service
public class ManLeaveServiceImpl extends ServiceImpl<ManLeaveMapper, ManLeave> implements ManLeaveService {


    @Autowired
    private SendMessageFeignClient sendMessageFeignClient;
    @Override
    public boolean saveLeave(JSONObject params) {
        ManLeave manLeave = params.toJavaObject(ManLeave.class);
        manLeave.setUserId(JwtUtil.getUserId());
        manLeave.setLeaveName(JwtUtil.getUsername());
        manLeave.setCreatePerson(JwtUtil.getUsername());
        manLeave.setUpdatePerson(JwtUtil.getUsername());
        manLeave.setLeaveStatus(LeaveStatusEnum.EXAMINATION_AND_APPROVAL.getCode());
        int insert = this.baseMapper.insert(manLeave);
        if(insert>0){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("noticeInfo",params.getString("leaveInfo"));
            jsonObject.put("resource",2);
            jsonObject.put("subtitle","请假");
            jsonObject.put("username",JwtUtil.getUsername());
            jsonObject.put("userIds",params.getString("currentDeptUserId")+","+params.getString("parentDeptUserId"));
            sendMessageFeignClient.sendMessage(jsonObject);
            return true;
        }
        return false;
    }

    @Override
    public Page<ManLeave> list(Page<ManLeave> page, ManLeave manLeave) {
        LambdaQueryWrapper<ManLeave> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManLeave::getDeleted,new Integer(0));
        wrapper.eq(ManLeave::getUserId,JwtUtil.getUserId());
        wrapper.orderByDesc(ManLeave::getCreateTime);
        if(StrUtil.isNotEmpty(manLeave.getLeaveName())){
            wrapper.like(ManLeave::getLeaveName,manLeave.getLeaveName());
        }
        return this.baseMapper.selectPage(page,wrapper);
    }
}
